<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\sw_manager;
use App\sw_role;
use App\sw_auth;
use DB;
use App\log;
use Zhuzhichao\IpLocationZh\Ip;
class ManageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function login()
    {  
        return view('Admin.login');
    }
    public function loginpost(Request $req)
    {
      $mg=sw_manager::where('mg_name',"$req->mg_name")->first();//查询是否有这个用户名

      if ($mg==null) {
          return '用户名不正确';
      }

      if ($mg->mg_pwd==md5($req->mg_pwd.$mg->salt)) {//当密码校验正确时
            $req->session()->put('mg_name',$req->mg_name);//将用户名存入session数组中
            $mg->last_time=time();//插入登陆时间
            $mg->save();
            //存入登陆日记
            $log=new log;
            $log->mg_name=$req->mg_name;
            $log->last_time=time();//插入登陆时间
            $log->role_id=$mg->mg_role_id;

            $log->ip=$req->ip();

            $log->address=implode('',Ip::find($log->ip));

            $log->save();

            return redirect('Admin/index');//登陆成功跳转首页
      }else{
        return '密码错误';
      }
      
    }
      //退出
    public function logout(Request $req){
        $req->session()->put('mg_name',null);//销毁登陆
        return redirect('Admin/login');
    }

    //管理员列表
    
  public function mglist(){

    $mg=sw_manager::join('sw_roles','sw_managers.mg_role_id','=','sw_roles.role_id')->select('sw_managers.*','sw_roles.role_name')->get();//联表查出所有管理员
    $role=sw_role::get();//查出所有角色
    return view('Admin.administrator',['mg'=>$mg,'role'=>$role]);
  }

  //添加管理员
  public function mglistpost(Request $req){
    $mg=new sw_manager;
    $mg->mg_name=$req->mg_name;
    $salt= str_random(6);
    $mg->mg_pwd=md5($req->mg_pwd.$salt);
    $mg->salt=$salt;
    $mg->mg_time=time();
    $mg->gender=$req->gender;
    $mg->mg_role_id=$req->mg_role_id;
    $mg->mobile=$req->mobile;
    $mg->email=$req->email;
    $mg->jieshao=$req->jieshao;
    if($mg->save()){
      DB::table('sw_roles')->where('role_id',$req->mg_role_id)->increment('num');//所属角色num字段加一
      return back()->withInput();
    }
  }

  //删除管理员
  public function mgdel($mg_id){
    $mm=sw_manager::where('mg_id',$mg_id)->first();
    $ro=$mm->mg_role_id;
    

    if($mm->delete()){
        DB::table('sw_roles')->where('role_id',$ro)->decrement('num');
      return back();
    }
  }

 // 修改管理员
  public function mgedit($mg_id){
    $role=sw_role::get();
    $mg=sw_manager::find($mg_id);
    return view('Admin.admin_edit',['mg'=>$mg,'role'=>$role]);
  }

  //修改管理员
   public function mgeditpost(Request $req,$mg_id){  
    $mg=sw_manager::find($mg_id);
    $ro=$mg->mg_role_id;
    $mg->mg_name=$req->mg_name;
    $salt= str_random(6);
    $mg->mg_pwd=md5($req->mg_pwd.$salt);
    $mg->salt=$salt;
    $mg->gender=$req->gender;
    $mg->mg_role_id=$req->mg_role_id;
    $mg->mobile=$req->mobile;
    $mg->email=$req->email;
    $mg->jieshao=$req->jieshao;
    if($mg->save()){
 //判断角色是否修改
       if ($req->role_id!=$ro) {
      DB::table('sw_roles')->where('role_id',$ro)->decrement('num');
      DB::table('sw_roles')->where('role_id',$req->mg_role_id)->increment('num');
      }
      return back()->withInput();
    }
  }

  //角色列表
   public function mgrole(){
    $role=sw_role::all();
    return view('Admin.admin_Competence',['role'=>$role]);
  }

  //添加角色
  public function Competence(){
    $authA=sw_auth::where('auth_level','1')->get();
    $authB=sw_auth::where('auth_level','2')->get(); 
    $authC=sw_auth::where('auth_level','3')->get();
    return view('Admin.Competence',['authA'=>$authA,'authB'=>$authB,'authC'=>$authC]);
  }

  //添加角色
  public function Competencepost(Request $req){
     
      $role=new sw_role;
      $role->role_name=$req->role_name;
      $role->jieshao=$req->jieshao;
      $role->auth_id=implode(',',$req->auth_id);
     $zz='';
      foreach ($req->auth_id as $v) {
          $au=sw_auth::find($v);
          if ($au->auth_luyou!='') {
            $zz.=$au->auth_luyou.',';
          } 
      }
      $role->auth_luyou=$zz;
      if($role->save()){
        return back()->withInput();
      }
  }

//删除角色
  public function mgroledel($role_id){
    $role=sw_role::find($role_id);
    if ($role->num!=0) {
      return '角色下有管理员请先删除管理员';
    }
    sw_role::where('role_id',$role_id)->delete();
    return back();
  }

//修改角色
  public function Competence2($role_id){
    $authA=sw_auth::where('auth_level',1)->get();
    $authB=sw_auth::where('auth_level',2)->get();
    $authC=sw_auth::where('auth_level',3)->get();
    $role=sw_role::find($role_id);
    return view('Admin.Competence2',['authA'=>$authA,'authB'=>$authB,'authC'=>$authC,'role'=>$role]);
  }

  //修改角色
  public function Competence2post(Request $req,$role_id){
      $role=sw_role::find($role_id);
      $role->role_name=$req->role_name;
      $role->jieshao=$req->jieshao;
      $role->role_auth_ids=implode(',',$req->auth_id);
      if($role->save()){
        return back()->withInput();
      }
  }

//登陆日志
   public function mglog(){
   $log= log::join('sw_roles','logs.role_id','=','sw_roles.role_id')->select('logs.*','sw_roles.role_name')->get();
    return view('Admin.admin_info',['log'=>$log]);
  }

    //权限列表
    public function authlist(){
        $authA=DB::table('sw_auths')->where('auth_level','1')->get();
        $authB=DB::table('sw_auths')->where('auth_level','2')->get();
        $authC=DB::table('sw_auths')->where('auth_level','3')->get();
        return view('Admin.authlist',['authA'=>$authA,'authB'=>$authB,'authC'=>$authC]);
    }
    public function authadd(Request $req){
        $auth=new sw_auth;
        if ($req->authA_id=='0') {
            $auth->auth_level=1;
            $auth->auth_pid=0;
        }else{
           if ($req->authB_id=='0') {
             $auth->auth_level=2;
             $auth->auth_pid=$req->authA_id;
           }else{
               $auth->auth_level=3;
             $auth->auth_pid=$req->authB_id;
           }
        }
        $auth->auth_name=$req->auth_name;
        $auth->auth_luyou=$req->auth_luyou;
        if ($auth->save()) {
          return back();
        }
       
    }

}
